package com.dpac.core.exception;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;

import com.dpac.core.bean.CommonBean;
import com.dpac.core.vo.BaseVo;



/**
 * @title 输出异常到控制台映射
 * @description 将异常信息输出到控制台
 * @creator chenwq
 * @date 2017年5月3日 下午4:52:51
 */
public class OutputConsoleMappingExceptionResolver extends SimpleMappingExceptionResolver {

	// private final static Logger LOGGER = LoggerFactory.getLogger(OutputConsoleMappingExceptionResolver.class);

	@Override
	protected ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception exception) {
		String sCause = exception.getCause().toString();
		if (sCause.indexOf("AuthorizationException") != -1) {
			Object[] objAccount = CommonBean.getFieldValue(request.getSession().getAttribute("AdminAccount"), BaseVo.class, "account");
			if ((int) objAccount[1] != -1 && !StringUtils.isEmpty(CommonBean.objectToString(objAccount[0]))) {
				System.out.println(CommonBean.getNowTime() + "账号：" + objAccount[0] + "，访问地址为：" + CommonBean.getPageUrl(request) + request.getRequestURI() + "，访问方法为：" + handler + "，出现权限不足。原因是："
						+ exception.getMessage());
			} else {
				System.out.println(CommonBean.getNowTime() + "访问地址为：" + CommonBean.getPageUrl(request) + request.getRequestURI() + "，访问方法为：" + handler + "，出现权限不足。原因是：" + exception.getMessage());
			}
		} else {
			exception.printStackTrace();
		}
		return super.doResolveException(request, response, handler, exception);
	}
}